package com.linian.pushforcampus.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.linian.pushforcampus.domain.CampusActivity;
import com.linian.pushforcampus.domain.PushConfigure;
import com.linian.pushforcampus.domain.WechatAccessToken;
import com.linian.pushforcampus.service.PushingService;
import com.linian.pushforcampus.service.TaskService;
import com.linian.pushforcampus.service.WechatAccessTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.linian.pushforcampus.util.HttpUtil.*;

/**
 * packageName com.linian.pushforcampus.service.Impl
 *
 * @author Lennon
 * @className TaskServiceImpl
 * @date 2023/11/11
 * @description TODO
 */

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {
    @Autowired
    private PushConfigure pushConfigure;


    @Autowired
    private WechatAccessTokenService wechatAccessTokenService;





    @Override
    public void updateAccessToken() {
        JSONObject json = new JSONObject();
        json.put("grant_type", "client_credential");
        json.put("appid", pushConfigure.getAppId());
        json.put("secret", pushConfigure.getSecret());
        String jsonString = json.toString();
        try {
            //解析请求的json，获取json数据。
            JSONObject jsonObject = JSONObject.parseObject(sendPostRequest(pushConfigure.getACCESSTOKENURL(), jsonString));
            String accessToken = jsonObject.getString("access_token");
            String expiresIn = jsonObject.getString("expires_in");
            //更新到数据库中,最后五分钟token将会更换,需要保证token是最新的
            wechatAccessTokenService.updateWechatAccessToken(new WechatAccessToken(1, accessToken));
            log.info("更换token:" + expiresIn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public void forceRefreshAccessToken() {
        JSONObject json = new JSONObject();
        json.put("grant_type", "client_credential");
        json.put("appid", pushConfigure.getAppId());
        json.put("secret", pushConfigure.getSecret());
        json.put("force_refresh", true);
        String jsonString = json.toString();
        try {
            //解析请求的json，获取json数据。
            JSONObject jsonObject = JSONObject.parseObject(sendPostRequest(pushConfigure.getACCESSTOKENURL(), jsonString));
            if (jsonObject.containsKey("errcode")) {
                log.info("强制刷新失败");
            } else {
                String accessToken = jsonObject.getString("access_token");
                //更新到数据库中
                wechatAccessTokenService.updateWechatAccessToken(new WechatAccessToken(1, accessToken));
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JSONObject postActApply(String studentId, String publishId, String token) {
        long currentTimestamp = System.currentTimeMillis();
        JSONObject json = new JSONObject();
        json.put("studentId", studentId);
        json.put("publishId", publishId);
        json.put("timeStamp", currentTimestamp);
        json.put("endPoint", "app");
        String jsonString = json.toString();
        JSONObject jsonObject = null;
        String strOut = null;
        try {
            strOut = sendActApplyRequest(pushConfigure.getACTAPPLYURL(), jsonString, token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        jsonObject = JSONObject.parseObject(strOut);
        return jsonObject;
    }


}
